# -*- coding: utf-8 -*-
# @Author: fanmingxiang@touty.io
# @Date: 2023/3/31 22:10
import json
from django.shortcuts import render
from django.http import JsonResponse, HttpResponse
# Create your views here.
from apitests.models import ApiDefine, ApiCase, ApiAutoTest, CaseTemporary
from common.pagination import Pagination
from projects.models import Projects, Modules
from pure_pagination import Paginator, EmptyPage, PageNotAnInteger


def api_case_list(request):
    try:
        page = request.GET.get('page', 1)
    except PageNotAnInteger:
        page = 1
    nums = request.GET.get('nums', None)
    search_text = request.GET.get('Search', '')
    search_dict = {}
    projects_list = Projects.objects.all().order_by('-create_time')
    is_project = int(request.COOKIES['Project'])
    if search_text:
        search_dict['name__contains'] = search_text
    search_dict['api_define__module__project'] = is_project
    api_cases_list = ApiCase.objects.filter(**search_dict).order_by('api_define', '-create_time')
    content = Pagination(api_cases_list, search_text, page, nums).per_page()
    content['projects_list'] = projects_list
    content['is_project'] = is_project
    return render(request, 'api_case_list.html', content)


def api_case_del(request):
    api_case_id = request.POST['api_case_id']
    ApiCase.objects.filter(id=api_case_id).delete()
    return JsonResponse({"status": True})


def api_case_edit(request):
    if request.method == 'GET':
        projects_list = Projects.objects.all().order_by('-create_time')
        is_project = int(request.COOKIES['Project'])
        api_case_id = request.GET.get('api_case_id')
        api_case = ApiCase.objects.get(id=api_case_id)
        params = json.loads(api_case.params)
        headers = json.loads(api_case.headers)
        body = json.loads(api_case.body)
        content = {
            'api_case': api_case,
            "params": params,
            "headers": headers,
            "body": body,
            "projects_list": projects_list,
            "is_project": is_project,

        }
        return render(request, 'api_case_edit.html', content)
    is_assert = request.POST['is_assert']
    api_id = request.POST['api_id']
    api_case_name = request.POST['api_case_name']
    api_params = request.POST['api_params']
    api_headers = request.POST['api_headers']
    body_choice = request.POST['body_choice']
    api_body = request.POST['api_body']
    assert_choice = request.POST['assert_choice']
    api_assert = request.POST['api_assert']
    if (api_case_name):
        ApiCase.objects.filter(id=api_id).update(name=api_case_name,
                                                 params=api_params,
                                                 headers=api_headers,
                                                 body_type=body_choice, body=api_body,
                                                 assert_type=assert_choice, assert_body=api_assert,
                                                 is_assert=is_assert)
        return JsonResponse({"status": True})

    return JsonResponse({"status": False, "error_msg": "信息未填写完整"})
